/**
 * 
 */
package org.cuatrovientos.kart;

import java.util.Vector;

/**
 * @author berrade
 *
 */
public class Race {
	
	private String name;
	private Circuit myCircuit;
	private Vector <Car> participants = new Vector<Car>();
	
	
	/**
	 * Constructor for name and one circuit
	 * 
	 * @param name
	 * @param myCircuit
	 */
	public Race(String name, Circuit myCircuit) {
		super();
		this.name = name;
		this.myCircuit = myCircuit;
	}
	
	
	
     /**
      * Add vehicle
      * 
      * @param v
      */
    public void addCar(Car v){
           
            participants.add(v);
            
    }

    /**
     *  calculates time for a car to finish the circuit
     * 
     * @param v
     * @return
     */
    private int time(Car v){
     
    	int seconds=0;
    	int km=0;
    	
     
     while (km < myCircuit.getKm()){
             seconds++;
             km+=v.move();
     }
     return seconds;
     }

    
    /**
     *  gets the faster car !!
     *  
     * @return
     */
    
    public Car run(){
        int carSeconds=0;
        int faster=500000;
        int aux=0;
                       
        
        
        for (int i=1;i< participants.size();i++){
        	
        	carSeconds=time(participants.get(i));
        	
        	if (carSeconds<faster){
               
               faster=carSeconds;
               aux=i;
               
        	}
        }
        
        return participants.get(aux);    
    }

    
    
    
}
